package com.wenhuitec.productlib.ui.activity;

import android.content.Context;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gzwh.weblib.bean.NzProductCategory;
import com.gzwh.weblib.bean.Result;
import com.gzwh.weblib.utils.MyObserver;
import com.gzwh.weblib.utils.RequestUtils;
import com.wenhuitec.productlib.R;
import com.wenhuitec.productlib.ui.adapter.CategorySortAdapter;
import com.wenhuitec.productlib.ui.adapter.MenuCategoryAdapter;
import com.wenhuitec.sysui.TitleBuilder;
import com.wenhuitec.sysui.recyclerviewHelper.MyItemTouchHelperCallback;
import com.wenhuitec.sysui.recyclerviewHelper.OnStartDragListener;

import java.util.List;

public class CategorySortActivity extends AppCompatActivity {
    private ItemTouchHelper mItemTouchHelper;

    private Context mContext;

    private CategorySortAdapter categorySortAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_category_sort);
        new TitleBuilder(this)
                .setTitleText("分类排序")
//                .setLeftIco(R.drawable.yp)
                .setLeftIcoListening((view) -> finish());
        mContext = this;
        RecyclerView mLv = (RecyclerView) findViewById(R.id.cateRv);
        mLv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));


        RequestUtils.getCategoryList(mContext, new MyObserver<List<NzProductCategory>>(mContext, false) {
            @Override
            public void onSuccess(Result<List<NzProductCategory>> result) {
                mLv.setLayoutManager(new LinearLayoutManager(mContext));//设置布局管理器
                categorySortAdapter = new CategorySortAdapter(mContext, result.getResult(), onStartDragListener);
                mLv.setAdapter(categorySortAdapter);

                categorySortAdapter.setOnClickSwitchListener(new CategorySortAdapter.OnClickSwitchListener() {
                    @Override
                    public void onClick(int position, boolean isChecked) {
                        //实际开发中做发送请求等等一些处理
                    }
                });
                mItemTouchHelper = new ItemTouchHelper(new MyItemTouchHelperCallback(categorySortAdapter));

                mItemTouchHelper.attachToRecyclerView(mLv);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {

            }
        });
    }

    OnStartDragListener onStartDragListener = new OnStartDragListener() {
        @Override
        public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
            //通知ItemTouchHelper开始拖拽
            mItemTouchHelper.startDrag(viewHolder);
        }
    };

}
